package com.yeyks.controller.hotel;


import com.yeyks.account.dal.domain.AccountInfo;
import com.yeyks.common.ajax.AjaxResponseBody;
import com.yeyks.common.exception.ServiceException;
import com.yeyks.commonReference.service.account.AccountInfoService;
import com.yeyks.commonReference.service.hotel.HotelInfoService;
import com.yeyks.hotel.dal.domain.HotelInfo;
import com.yeyks.hotel.param.HotelCommissionBillListParam;
import com.yeyks.hotel.service.HotelCommissionBillService;
import com.yeyks.hotel.service.HotelCommissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 酒店佣金表 前端控制器
 * </p>
 *
 * @author yun
 * @since 2019-05-27
 */
@Api(tags = "酒店佣金接口信息", description = "酒店佣金接口信息")
@RestController
@RequestMapping("/ajax/hotelCommission")
public class HotelCommissionController {

    @Autowired
    private HotelCommissionService hotelCommissionService;
    @Autowired
    private HotelCommissionBillService hotelCommissionBillService;
    @Autowired
    private AccountInfoService accountService;
    @Autowired
    private HotelInfoService hotelService;

    @ApiOperation(value = "佣金统计数据", notes = "佣金统计数据")
    @RequestMapping(value = "/getStatisticsData", method = RequestMethod.POST)
    public AjaxResponseBody getStatisticsData(String token) {
        if (StringUtils.isBlank(token)) {
            throw new ServiceException("token不存在");
        }
        //account信息
        AccountInfo accountInfo = null;
        try {
            accountInfo = accountService.token2AccountInfo(token);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (null == accountInfo) {
            throw new ServiceException("用户信息错误");
        }
        //如果是子账户就使用父账户的id进行
        Integer accountId = accountInfo.getId();
        if (null != accountInfo.getManagerId()) {
            accountId = accountInfo.getManagerId();
        }
        //获取酒店信息
        List<HotelInfo> hotelInfos = hotelService.searchHotelsByOperator(accountId);
        if (null == hotelInfos || hotelInfos.size() < 1) {
            throw new ServiceException("用户信息错误");
        }
        HotelInfo hotelInfo = hotelInfos.get(0);
        Integer hotelInfoId = hotelInfo.getId();
        return hotelCommissionService.getStatisticsData(hotelInfoId);
    }

    @ApiOperation(value = "佣金明细列表(分页)", notes = "佣金明细列表(分页)")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public AjaxResponseBody list(HotelCommissionBillListParam hotelCommissionBillListParam) {
        String token = hotelCommissionBillListParam.getToken();
        if (StringUtils.isBlank(token)) {
            throw new ServiceException("token不存在");
        }
        //account信息
        AccountInfo accountInfo = null;
        try {
            accountInfo = accountService.token2AccountInfo(token);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (null == accountInfo) {
            throw new ServiceException("用户信息错误");
        }
        //如果是子账户就使用父账户的id进行
        Integer accountId = accountInfo.getId();
        if (null != accountInfo.getManagerId()) {
            accountId = accountInfo.getManagerId();
        }
        //判断用户类型
        switch (accountInfo.getType()) {
            case 5:
                //获取酒店信息
                List<HotelInfo> hotelInfos = hotelService.searchHotelsByOperator(accountId);
                if (null == hotelInfos || hotelInfos.size() < 1) {
                    throw new ServiceException("用户信息错误");
                }
                HotelInfo hotelInfo = hotelInfos.get(0);
                Integer hotelInfoId = hotelInfo.getId();
                hotelCommissionBillListParam.setHotelId(hotelInfoId);
                break;
            case 1:
                break;
            default:
                throw new ServiceException("不允许访问的接口");
        }
        return hotelCommissionBillService.list(hotelCommissionBillListParam);
    }

}
